package com.xgr.wonderful.adapter;

import java.util.List;

import android.R.animator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.xgr.wonderful.MyApplication;
import com.xgr.wonderful.R;
import com.xgr.wonderful.activity.CommentActivity;
import com.xgr.wonderful.activity.GalleryActivity;
import com.xgr.wonderful.activity.PersonalCenterActivity;
import com.xgr.wonderful.activity.PersonalCenterActivity2;
import com.xgr.wonderful.constant.Constant;
import com.xgr.wonderful.entity.Photo;
import com.xgr.wonderful.entity.Status;
import com.xgr.wonderful.entity.User;
import com.xgr.wonderful.sns.SharePopWindow;
import com.xgr.wonderful.utils.DisplayUtils;
import com.xgr.wonderful.utils.HttpProxy;
import com.xgr.wonderful.utils.LocalLikeRelationsUtils;
import com.xgr.wonderful.utils.LocalUserUtils;
import com.xgr.wonderful.utils.LogUtils;
import com.xgr.wonderful.utils.ToastFactory;
import com.xgr.wonderful.utils.HttpProxy.IHttpRequestListener;
import com.xgr.wonderful.view.CircleImageView;

public class StatusContentAdapter extends BaseContentAdapter<Status> {
	
	private Activity activity = null;
	private User mUser =null;
	private final String TAG ="StatusContentAdapter" ;
	
	private Animation scaleAnimation;// for like layout 
	
	public StatusContentAdapter(Context context, List<Status> list) {
		super(context, list);
		this.activity = (Activity)context;
		if(mUser == null){
			mUser = LocalUserUtils.getInstance().getLocalUser() ;
		}
		//get the animation 
		scaleAnimation = AnimationUtils.loadAnimation(context, R.anim.scale_anim) ;//for like image
	}

	@Override
	public View getConvertView(int position, View convertView, ViewGroup parent) {
		ViewHolder  viewHolder = null ;
		if (convertView == null ){
			viewHolder = new ViewHolder() ;
			convertView = mInflater.inflate(R.layout.item_status_homepage1,
					null);
			viewHolder.mAvatar = (CircleImageView) convertView
					.findViewById(R.id.item_homepage_userphoto);
			viewHolder.mUsername = (TextView) convertView
					.findViewById(R.id.item_homepage_username);
			viewHolder.mTime = (TextView) convertView
					.findViewById(R.id.item_homepage_time);
			viewHolder.mDistance = (TextView) convertView
					.findViewById(R.id.item_homepage_distance);
			viewHolder.mContent = (TextView) convertView
					.findViewById(R.id.item_homepage_content);
			viewHolder.mImage = (ImageView) convertView
					.findViewById(R.id.item_homepage_image);
			//comment
			viewHolder.mCommentLL=(LinearLayout) convertView
					.findViewById(R.id.item_homepage_comment);
			viewHolder.mCommentTv= (TextView)convertView
					.findViewById(R.id.item_homepage_comment_tv) ;
			//like 
			viewHolder.mLikeLL = (LinearLayout)convertView
					.findViewById(R.id.item_homepage_like);
			viewHolder.mLikeImg= (ImageView)convertView
					.findViewById(R.id.item_homepage_like_img);
			viewHolder.mLikeTv= (TextView)convertView
					.findViewById(R.id.item_homepage_like_tv) ;
			//share
			viewHolder.mShareLL = (LinearLayout)convertView
					.findViewById(R.id.item_homepage_share) ;
			convertView.setTag(viewHolder);
		}else {
			viewHolder = (ViewHolder)convertView.getTag() ;
		}
		Status curStatus = dataList.get(position) ;
		handleData (curStatus, viewHolder, position);
		return convertView;
	}
	
	private void handleData(final Status curStatus, final ViewHolder viewHolder,
			int position) {
		if (curStatus.getUser() != null){
			viewHolder.mUsername.setText(curStatus.getUser().getUsername()+"");
		}
		ImageLoader.getInstance().displayImage(
				curStatus.getUser().getPhoto()!= null ? curStatus.getUser().getPhoto().getSmall() : null ,
						viewHolder.mAvatar,
						MyApplication.getInstance().getOptions(R.drawable.user_icon_default) ,
								new SimpleImageLoadingListener()) ;
		//time 
		viewHolder.mTime.setText(DisplayUtils.transformTime(curStatus.getTime())) ;
		LogUtils.i(TAG, curStatus.getTime().toString()) ;
		//distance 
		if (curStatus.getDistance()!= null){
			viewHolder.mDistance.setVisibility(View.VISIBLE) ;
			viewHolder.mDistance.setText(DisplayUtils.transformDistance(curStatus.getDistance())) ;
		}else {
			viewHolder.mDistance.setVisibility(View.GONE) ;
		}
		//content 
		if (curStatus.getContent()!= null && curStatus.getContent().length()>0){
			viewHolder.mContent.setVisibility(View.VISIBLE) ;
			viewHolder.mContent.setText(curStatus.getContent()+"") ;
		}else {
			viewHolder.mContent.setVisibility(View.GONE) ;
		}
		// image 
		final List<Photo> imageList = curStatus.getImageurl() ;
		if (null != imageList && imageList.size()!= 0 && imageList.get(0)!= null
				&& imageList.get(0).getUrl()!= null){
			viewHolder.mImage.setVisibility(View.VISIBLE) ;
			ImageLoader.getInstance().displayImage(
					imageList.get(0).getUrl(), 
					viewHolder.mImage ,
					MyApplication.getInstance().getOptions(R.drawable.bg_pic_loading)) ;
		}else {
			viewHolder.mImage.setVisibility(View.GONE) ;
		}
		//like 
		curStatus.setIsLiked(LocalLikeRelationsUtils.getInstance().isLiked(curStatus.getId())) ;
		viewHolder.mLikeTv.setText(curStatus.getUp()+"");
		
		if (curStatus.getIsliked()){
			viewHolder.mLikeImg.setImageResource(R.drawable.timeline_icon_liked) ;
		}else {
			viewHolder.mLikeImg.setImageResource(R.drawable.timeline_icon_unlike) ;
		}
		//comment 
		viewHolder.mCommentTv.setText(curStatus.getCommentnum()+"") ;
		//clik on avatar, goto personal center
		viewHolder.mAvatar.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent () ;
				if (curStatus.getUser().getID().intValue()!= mUser.getID().intValue()){
					intent.setClass(mContext, PersonalCenterActivity2.class) ;
				}else {
					intent.setClass(mContext, PersonalCenterActivity.class) ;
				}
				intent.putExtra("data", curStatus.getUser()) ;
				activity.startActivity(intent) ;
			}
		}) ;
		//click on image, goto comment,but now is for test
		viewHolder.mImage.setOnClickListener(new OnClickListener() {	
			@Override
			public void onClick(View v) {
				Intent intent = new Intent () ;
				intent.setClass(mContext, GalleryActivity.class) ;
				intent.putExtra("data",  curStatus) ;
				activity.startActivity(intent) ;
			}
		}) ;
		
		//share 
		viewHolder.mShareLL.setOnClickListener(new OnClickListener() {	
			@Override
			public void onClick(View v) {
				String targetUrl = Constant.SHARE_OFFICIAL_URL+"/p.html?id="+curStatus.getId() ;
				String imageUrl = null;
				if (imageList != null && imageList.size()!=0 && imageList.get(0)!=null
						&& imageList.get(0).getUrl()!= null){
					imageUrl = imageList.get(0).getUrl() ;
				}

				SharePopWindow mShare = new SharePopWindow(mContext,
						curStatus.getContent(),
						Constant.SHARE_TITLE, 
						targetUrl,
						imageUrl, 
						null) ;
				mShare.showAtLocation(activity.getWindow()
						.getDecorView(), Gravity.BOTTOM, 0, 0);
			}
		}) ;
		//comment,not completed
		viewHolder.mCommentLL.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent() ;
				intent.putExtra("data", curStatus) ; 
				intent.setClass(activity,CommentActivity.class) ;
				activity.startActivity(intent) ;
			}
		}) ;
		// like 
		viewHolder.mLikeLL.setOnClickListener(new OnClickListener() {	
			@Override
			public void onClick(View v) {
				doLikeClick(viewHolder , curStatus); 
				viewHolder.mLikeImg.startAnimation(scaleAnimation);
			}
		}) ;
		
	}

	protected void doLikeClick(final ViewHolder viewHolder, final Status curStatus) {
		if (curStatus.getIsliked()){
			ToastFactory.getToast(mContext,"已经点赞过了").show();
			return ;
		}
		Status temp = new Status(mUser.getID(), curStatus.getId()) ;
		String statusJson = new Gson().toJson(temp) ;
		HttpProxy httpProxy = new HttpProxy() ;
		
		httpProxy.doLikeAction(statusJson, new IHttpRequestListener() {
			
			@Override
			public void OnHttpRequestSuccessful(String result, int errCode) {
				LogUtils.i(TAG, "result:"+result) ;
				if (errCode== Constant.ERR_HTTP_REQUEST_SUCCESS){
					ToastFactory.getToast(mContext, "点赞成功").show() ;
					curStatus.increaseUp() ; //refresh local status list 
					LocalLikeRelationsUtils.getInstance().addLikeRelations(curStatus.getId()) ;
					//立即保存
					LocalLikeRelationsUtils.getInstance().saveLikeRelations();
					LogUtils.i(TAG, "set color for text:"+ curStatus.getUp());
					viewHolder.mLikeImg.setImageResource(R.drawable.timeline_icon_liked) ;
					//viewHolder.mLike.setTextColor(Color.parseColor("#D95555"));
					viewHolder.mLikeTv.setText(curStatus.getUp()+"") ;//一定要加""
					notifyDataSetChanged() ;
				}else if (errCode == Constant.ERR_HTTP_REQUEST_FAILURE) {
					//参数 错误的中在上面已经排除了
					ToastFactory.getToast(mContext, "点赞失败 result:"+result).show();
				}
			}
			
			@Override
			public void OnHttpRequestStart() {
	
			}
			
			@Override
			public void OnHttpRequestFailure(String info) {
				ToastFactory.getToast(mContext, "点赞失败").show() ;
			}
		}) ;
		
		
	}

	private class ViewHolder {
		public CircleImageView mAvatar;
		public TextView mUsername;
		public TextView mTime;
		public TextView mDistance;

		public TextView mContent;
		public ImageView mImage;
		//like 
		public LinearLayout mLikeLL;
		public TextView mLikeTv;
		public ImageView mLikeImg;
		//share
		public LinearLayout mShareLL;
		//comment
		public LinearLayout mCommentLL;
		public TextView mCommentTv;

	}

}
